package sample.stock;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ChartView extends Activity {
    private WebView webView = null;
    private String symbol = "";
    private String size;
    private String time;
    private String type;
    private String scale;
    private String movAvg;
    private String expAvg;
    private String IDs;
   /* private static final String day_1 = " http://ichart.finance.yahoo.com/b?s="; // 1 day
    private static final String day_5 = " http://ichart.finance.yahoo.com/w?s="; // 5 days
    private static final String month_3 = "http://chart.finance.yahoo.com/c/3m/"; // 3 months
    private static final String month_6 = "http://chart.finance.yahoo.com/c/6m/";
    private static final String year_1 = "http://chart.finance.yahoo.com/c/1y/";
    private static final String year_2 = "http://chart.finance.yahoo.com/c/2y/";
    private static final String year_5 = "http://chart.finance.yahoo.com/c/5y/";
    private static final String max = "http://chart.finance.yahoo.com/c/my/";*/
    private static final String base_URL = "http://ichart.yahoo.com/";
    
    final Activity activity = this;
	 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.chartview);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        webView = (WebView)findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
 
                if(progress == 100)
                    activity.setTitle("Chart Views");
            }
        });
        
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }
 
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
        
        Bundle extras = getIntent().getExtras(); 
        symbol = extras.getString("symbol");
        size = extras.getString("size");
        time = extras.getString("time");
        type = extras.getString("type");
        scale = extras.getString("scale");
        movAvg = extras.getString("movAvg");
        expAvg = extras.getString("expAvg");
        IDs = extras.getString("IDs");
        
        String url = base_URL;
        url += size;
        url += "?s=" + symbol;
        url += "&t=" + time;
        url += "&q=" + type;
        url += "&l=" + scale;
        url += "&p=" + movAvg + "," + expAvg;
        if(!IDs.isEmpty())
        	url += "&c=" + IDs;
        
        webView.loadUrl(url);
    }
}
